import {
  CreateDeviceModelRequestVO,
  CreateDeviceOperateRequestVO,
  CreateDeviceTemplateRequestVO,
  CreateRepoCompRequestVO,
  DeviceModelItem,
  DeviceOperateItem,
  DeviceOperatePageResult,
  DeviceOperateQueryParam, DeviceTemplateGroupItem,
  DeviceTemplateItem,
  RepoCompEntity,
  RepoCompPageResult,
  RepoCompQueryParam,
} from './types';
import request from '@/utils/request';
import {AxiosPromise} from 'axios';

export function listCompEntityList(queryParams: RepoCompQueryParam): AxiosPromise<RepoCompPageResult> {
  return request({
    url: '/kernel/manager/repository/local/comp-list/page',
    method: 'post',
    data: {
      keyWord: queryParams.keyWord,
      compRepo: queryParams.compRepo,
      compType: queryParams.compType,
      pageNum: queryParams.pageNum,
      pageSize: queryParams.pageSize
    }
  });
}

export function queryCompEntity(id: number): AxiosPromise<RepoCompEntity> {
  return request({
    url: '/kernel/manager/repository/local/comp-list/entity',
    method: 'get',
    params: {id: id}
  });
}

export function createCompEntity(data: CreateRepoCompRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/comp-list/entity',
    method: 'post',
    data: data,
  });
}

export function updateCompEntity(data: CreateRepoCompRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/comp-list/entity',
    method: 'put',
    data: data,
  });
}


export function deleteCompEntity(compId: number) {
  return request({
    url: '/kernel/manager/repository/local/comp-list/entity',
    method: 'delete',
    params: {id: compId},
  });
}

export function uploadCompEntity(data: CreateRepoCompRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/comp-list/upload',
    method: 'post',
    data: data,
  });
}

export function syncCompEntity(data: CreateRepoCompRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/comp-list/sync',
    method: 'post',
    data: data,
  });
}


export function listOperateEntityPages(queryParams: RepoCompQueryParam): AxiosPromise<DeviceOperatePageResult> {
  return request({
    url: '/kernel/manager/repository/local/operate-list/page',
    method: 'post',
    data: {
      id: queryParams.compId,
      keyWord: queryParams.keyWord,
      pageNum: queryParams.pageNum,
      pageSize: queryParams.pageSize
    },
  });
}

/**
 * 添加实体
 *
 * @taskParam data
 */
export function createOperateEntity(data: CreateDeviceOperateRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/operate-list/entity',
    method: 'post',
    data: data,
  });
}

export function updateOperateEntity(data: CreateDeviceOperateRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/operate-list/entity',
    method: 'put',
    data: {
      id: data.id,
      compId: data.compId,
      manufacturer: data.manufacturer,
      deviceType: data.deviceType,
      operateName: data.operateName,
      operateMode: data.operateMode,
      dataType: data.dataType,
      serviceType: data.serviceType,
      engineType: data.engineType,
      engineParam: data.engineParam,
      extendParam: data.extendParam,
      polling: data.polling,
      timeout: data.timeout,
    }
  });
}

export function deleteOperateEntity(ids: string) {
  return request({
    url: '/kernel/manager/repository/local/operate-list/entities',
    method: 'delete',
    params: {ids: ids},
  });
}

export function listOperateOptionList(queryParams: DeviceOperateQueryParam): AxiosPromise<OptionType[]> {
  return request({
    url: '/kernel/manager/repository/local/operate-list/option',
    method: 'post',
    data: queryParams,
  });
}

/**
 * 获取实体详情
 *
 * @taskParam id
 */
export function getOperateEntity(id: number): AxiosPromise<DeviceOperateItem> {
  return request({
    url: '/kernel/manager/repository/local/operate-list/entity',
    method: 'get',
    params: {id: id}
  });
}

export function listDeviceModelEntities(queryParams: RepoCompQueryParam): AxiosPromise<PageResult<DeviceModelItem[]>> {
  return request({
    url: '/kernel/manager/repository/local/jsn-list/page',
    method: 'post',
    data: {
      id: queryParams.compId,
      pageNum: queryParams.pageNum,
      pageSize: queryParams.pageSize
    }
  });
}

export function createDeviceModelEntity(data: CreateDeviceModelRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/jsn-list/entity',
    method: 'post',
    data: data,
  });
}

export function updateDeviceModelEntity(data: CreateDeviceModelRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/jsn-list/entity',
    method: 'put',
    data: {
      id: data.id,
      compId: data.compId,
      manufacturer: data.manufacturer,
      deviceType: data.deviceType,
      modelName: data.modelName,
      modelParam: data.modelParam,
      extendParam: data.extendParam,
    }
  });
}

export function deleteDeviceModelEntity(ids: string) {
  return request({
    url: '/kernel/manager/repository/local/jsn-list/entities',
    method: 'delete',
    params: {ids: ids},
  });
}

export function listDeviceTemplatePage(queryParams: RepoCompQueryParam): AxiosPromise<PageResult<DeviceTemplateItem[]>> {
  return request({
    url: '/kernel/manager/repository/local/device-template-list/page',
    method: 'post',
    data: {
      id: queryParams.compId,
      pageNum: queryParams.pageNum,
      pageSize: queryParams.pageSize,
      templateType: queryParams.templateType
    }
  });
}

export function createDeviceTemplateEntity(data: CreateDeviceTemplateRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/device-template-list/entity',
    method: 'post',
    data: data,
  });
}

export function updateDeviceTemplateEntity(data: CreateDeviceTemplateRequestVO) {
  return request({
    url: '/kernel/manager/repository/local/device-template-list/entity',
    method: 'put',
    data: {
      id: data.id,
      compId: data.compId,
      manufacturer: data.manufacturer,
      deviceType: data.deviceType,
      templateType: data.templateType,
      templateName: data.templateName,
      templateParam: data.templateParam,
      extendParam: data.extendParam,
    }
  });
}

export function deleteDeviceTemplateEntity(ids: string) {
  return request({
    url: '/kernel/manager/repository/local/device-template-list/entities',
    method: 'delete',
    params: {ids: ids},
  });
}

export function listDeviceTemplateOptions(queryParams: any): AxiosPromise<OptionType[]> {
  return request({
    url: '/kernel/manager//repository/local/device-template-list/option',
    method: 'post',
    data: {
      field: queryParams.field,
      manufacturer: queryParams.manufacturer,
      deviceType: queryParams.deviceType,
      templateType: queryParams.templateType,
      operateName: queryParams.operateName,
      subsetName: queryParams.subsetName,
      channelType: queryParams.channelType,
    },
  });
}

export function listDeviceTemplateEntities(queryParams: any): AxiosPromise<DeviceTemplateItem[]> {
  return request({
    url: '/kernel/manager/repository/local/device-template-list/entities',
    method: 'post',
    data: {
      manufacturer: queryParams.manufacturer,
      deviceType: queryParams.deviceType,
      templateType: queryParams.templateType,
      operateName: queryParams.operateName,
      subsetName: queryParams.subsetName,
      templateName: queryParams.templateName,
    }
  });
}

export function listDeviceTemplateGroup(id: number): AxiosPromise<DeviceTemplateGroupItem[]> {
  return request({
    url: '/kernel/manager/repository/local/device-template-list/group',
    method: 'get',
    params: {id: id}
  });
}
